package com.itheima.Myservlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 查看学生
 */
@WebServlet("/test03")
public class TestDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //创建字符输入流对象，关联读取的文件
        BufferedReader br =new BufferedReader(new FileReader("D://stu.txt"));
        //创建集合对象，用于保存student对象
        ArrayList<Student> list = new ArrayList<>();
        //循环读取文件中的数据，将数据封装到student对象中。再把多个学生对象添加到集合中
        String line;
        while ((line=br.readLine())!=null){
            Student stu =new Student();
            String[] arr = line.split(",");
            stu.setUsername(arr[0]);
            stu.setAge(Integer.parseInt(arr[1]));
            stu.setScore(Integer.parseInt(arr[2]));
            list.add(stu);
        }
        //将集合对象存入会话域中
        req.getSession().setAttribute("student",list);
        System.out.println(list);
        //重新定向到学生列表页面
        resp.sendRedirect("selectStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
